//
//  AirplaneVC.swift
//  旅航ios
//
//  Created by tulip on 2021/4/26.
//

import UIKit
import SwiftyJSON

class AirplaneVC: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    var airBannerData:[JSON]?
    var  backbtn:BackBtn!
    var collect:UICollectionView!
    
    var noti:String = ""
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
       
        self.view.backgroundColor=UIColor.init(hexString: "f2f1f6")
        
        let layout=UICollectionViewFlowLayout.init()
        layout.scrollDirection = .vertical
        
        layout.sectionInset=UIEdgeInsets(top: 0, left:15, bottom: 15, right: 15);
        
        collect=UICollectionView.init(frame: CGRect(x: 0, y: -StatusBarHeight, width: SCREEN_WIDTH, height: ScreenHight), collectionViewLayout: layout)
      
        self.view.addSubview(collect)
        collect.backgroundColor=UIColor.clear
        collect.bounces=false
        collect.delegate=self
        collect.dataSource=self
        collect.register(UINib(nibName: "Air_BannerCollectionCell", bundle: nil), forCellWithReuseIdentifier: "Air_BannerCollectionCell")
        collect.register(UINib(nibName: "Air_TeJiaCollectionCell", bundle: nil), forCellWithReuseIdentifier: "Air_TeJiaCollectionCell")
        collect.register(UINib(nibName: "SaleTicketListCell", bundle: nil), forCellWithReuseIdentifier: "SaleTicketListCell")
        collect.register(UINib(nibName: "Air_hengfuCollectCell", bundle: nil), forCellWithReuseIdentifier: "Air_hengfuCollectCell")
        collect.reloadData()
        

        backbtn=BackBtn.init(frame: CGRect(x: 15, y: StatusBarHeight+6, width: 33, height: 33))
        self.view.bringSubviewToFront(backbtn)
        self.view.addSubview(backbtn)
        
            

        Network.request(.airIndex) { (JSON) in
            if JSON["code"].int == 1 {
                self.airBannerData = JSON["data"]["airbanner"].arrayValue
//                self.collect.reloadData()
                self.getNotiMes()
            }
        } failure: { (err) in

        }
        
        

        
    }
    
    // 获取noti
    func getNotiMes(){
        Network.request(.notice) { (JSON) in
            if JSON["code"].int == 1 {
                
                var str:String = JSON["data"]["content"].string ?? ""
                
        
                self.noti = str.filterHTML()!
                
                print("\(self.noti)")
                self.collect.reloadData()
            }
        } failure: { (err) in
            
        }
    }
 
    
   
    
    
    
//    func getPriceCalendar(){
//
//        var dic:[String:String] = [:]
//
//        Network.request(.searchPriceByday(dic)) { (JSON) in
//            if JSON["code"].int == 1 {
//                self.noti = JSON["data"]["content"].string ?? ""
//                self.collect.reloadData()
//            }
//        } failure: { (err) in
//
//        }
//    }
    

    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        2
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0{
            return 1
        }
        if section == 1{
            return 1
        }
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
         
    }
        
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if indexPath.section == 0{
            let cell:Air_BannerCollectionCell=collectionView.dequeueReusableCell(withReuseIdentifier: "Air_BannerCollectionCell", for: indexPath) as! Air_BannerCollectionCell
            if airBannerData?.count ?? 0 > 0 {
                cell.notiBt.setTitle(noti, for: .normal)
                cell.propData=airBannerData
                cell.banner.reloadView()
            }
            return cell
        }
        if indexPath.section == 1{
            
            let cell:Air_hengfuCollectCell=collectionView.dequeueReusableCell(withReuseIdentifier: "Air_hengfuCollectCell", for: indexPath) as! Air_hengfuCollectCell
            
            return cell
        }
        
        let cell:SaleTicketListCell=collectionView.dequeueReusableCell(withReuseIdentifier: "SaleTicketListCell", for: indexPath) as! SaleTicketListCell
        
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        if indexPath.section == 0{
            return CGSize(width: SCREEN_WIDTH, height: 545)
        }
        if indexPath.section == 1{
            return CGSize(width: SCREEN_WIDTH-30, height:120)

        }
        return CGSize(width: (SCREEN_WIDTH-40)/2, height: 250)
    }

    
 //手势和tableview的selectc冲突
   
       func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
   
           if touch.view?.superview?.classForCoder == Air_BannerCollectionCell().classForCoder {
               return false
           }
       
           return true
       }

}

